/* 
 *  Copyright (C) 2006 Dip. Ing. dell'Informazione, University of Pisa, Italy
 *  http://info.iet.unipi.it/~cng/ns2measure/ns2measure.html
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA, USA
 */

/**
   project: measure
   filename: recover.cc
	author: C. Cicconetti <c.cicconetti@iet.unipi.it>
	year: 2006
   affiliation:
      Dipartimento di Ingegneria dell'Informazione
	   University of Pisa, Italy
   description:
		load and recover a saved data file
*/

#include <config.h>
#include <measure.h>
#include <input.h>

#include <cstdlib>
#include <unistd.h>

#include <set>
#include <iostream>
#include <string>

using namespace std;

void
printUsage (const char* progname) {
	cout << "usage: " << progname
		  << " savefile [-dhv] [-c conf] [-n name] [-s dir]\n"
		  << "options:\n"
		  << "   -c conf     use confidence level 'conf' (default = 0.95)\n"
		  << "   -n name     name of the scenario, used with '-s' option\n"
		  << "   -s dir      save dat files to directory 'dir'\n"
		  << "   -d          print pmf and cdf\n"
		  << "   -v          print license information\n"
		  << "   -h          print this help\n";
	exit (0);
}

void
printLicense () {
	cout
<< "Copyright (C) 2006 Dip. Ing. dell'Informazione, University of Pisa, Italy\n"
<< "http://info.iet.unipi.it/~cng/ns2measure/ns2measure.html\n"
<< "\n"
<< "This program is free software; you can redistribute it and/or modify\n"
<< "it under the terms of the GNU General Public License as published by\n"
<< "the Free Software Foundation; either version 2 of the License, or\n"
<< "(at your option) any later version.\n"
<< "\n"
<< "This program is distributed in the hope that it will be useful,\n"
<< "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
<< "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
<< "GNU General Public License for more details.\n"
<< "\n"
<< "You should have received a copy of the GNU General Public License\n"
<< "along with this program; if not, write to the Free Software\n"
<< "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA, USA\n";
	exit (0);
}

int
main (int argc, char* argv[]) {
	int ch;  // for parsing arguments
	bool printDistribution = false;
	string saveFileName;
	string savedir;
	string header;
	const char* progname = argv[0];
	double cl = 0.95;     // confidence level, default = 0.95

	// parse command-line arguments
	while ((ch = getopt (argc, argv, "dn:s:c:vh")) != -1) {
		switch (ch) {
			case 'd':
				printDistribution = true;
				break;
			case 'n':
				header = string(optarg);
				break;
			case 's':
				savedir = string(optarg);
				break;
			case 'c':
				cl = atof (optarg);
				break;
			case 'v':
				printLicense();   // does not return
			case 'h':
			default:
				printUsage(progname);     // does not return
		}
	}

	argc -= optind;
	argv += optind;

	if ( argc > 1 ) printUsage(progname);  // does not return
	
	if ( argc == 1 ) saveFileName = argv[0];

	try {
		Metrics metrics;
		Configuration conf;  // empty configuration
		Input input (conf, metrics);

		bool good = true;
		if ( argc == 0 ) input.readSingleRun (cin, 0, true);
		else good = input.recoverData (saveFileName);

		if ( good == true ) {
			if ( savedir.empty() ) {
				cout << "savefile is good!\n";
				const set<unsigned int>& ids = input.getRunIdentifiers ();
				set<unsigned int>::iterator it;
				cout << "set of run IDs: ";
				for ( it = ids.begin () ; it != ids.end() ; it++ ) {
					if ( it == ids.begin() )
						cout << *it;
					else
						cout << ' ' << *it;
				}
				cout << '\n';
				metrics.dump (cout, cl, printDistribution);
			} else {
				metrics.dump (savedir, header, cl, printDistribution);
			}
		} else {
			cout << "savefile is bad!\n"
				<< "old savefile has been saved to '" << saveFileName << ".old'\n"
				<< "recovered savefile is '" << saveFileName << "'\n";
		}

	} catch (Object& obj) {
		cerr << "Exception raised by the instance #" << obj.getId()
			<< " of class " << obj.getName() << ". Terminated.\n";
	}

	exit (1);
}
